//
//  TSSplicingTemplateListViewController2.m
//  XLCardSwitchDemo
//
//  Created by Apple on 2017/1/20.
//  Copyright © 2017年 Apple. All rights reserved.
// 

#import "TSSplicingTemplateListViewController2.h"
#import <Masonry/Masonry.h>
#import "BLSplicingTemplateList.h"
#import "SplicingTemplateCollectionViewCell.h"


#import "TSSplicingTemplateEditViewController.h"
#import "XCPresentationScaleAnimation.h"
#import "XCPresentation.h"

@interface TSSplicingTemplateListViewController2 ()<XLCardSwitchDelegate>

@property (nonatomic, strong) BLSplicingTemplateList *cardSwitch;


@end

@implementation TSSplicingTemplateListViewController2

- (instancetype)initWithImages:(NSArray<UIImage *> *)images {
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        _originImages = images;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = NSLocalizedString(@"选择模板", nil);
    [self setupViews];
    
    
    //设置卡片数据源
    self.cardSwitch.typeCount = 6;
    self.cardSwitch.images = self.originImages;
    
    //更新背景图
    [self configImageViewOfIndex:self.cardSwitch.selectedIndex];
}

- (void)setupViews {
    self.view.backgroundColor = [UIColor blackColor];
    
    UIView *bottomToolView = [[UIView alloc] init];
    //bottomToolView.backgroundColor = [UIColor magentaColor];
    [self.view addSubview:bottomToolView];
    [bottomToolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.mas_bottomLayoutGuide).mas_offset(-0);
        make.height.mas_equalTo(100);
    }];
    
    //初始化
    BLSplicingTemplateList *cardSwitch = [[BLSplicingTemplateList alloc] initWithFrame:self.view.bounds];
    cardSwitch.backgroundColor = [UIColor cyanColor];
    cardSwitch.delegate = self;         //设置代理方法
    cardSwitch.pagingEnabled = false;   //分页切换
    [self.view addSubview:cardSwitch];
    [cardSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view).mas_offset(0);
        make.top.mas_equalTo(self.mas_topLayoutGuide).mas_equalTo(0);
        make.bottom.mas_equalTo(bottomToolView.mas_top).mas_offset(-92);
    }];
    self.cardSwitch = cardSwitch;
}



//开关分页效果
- (void)segMethod:(UISegmentedControl *)seg {
    switch (seg.selectedSegmentIndex) {
        case 0:
            self.cardSwitch.pagingEnabled = false;
            break;
        case 1:
            self.cardSwitch.pagingEnabled = true;
            break;
        default:
            break;
    }
}

#pragma mark -
#pragma mark CardSwitchDelegate
-(void)cardSwitch_collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击了：%zd", indexPath.item);
    [self configImageViewOfIndex:indexPath.item];
    
    [self collectionView:collectionView didSelectItemAtIndexPath:indexPath];
}

- (void)cardSwitchDidScrollToIndex:(NSInteger)index {
    NSLog(@"滚动到了击了：%zd",index);
    [self configImageViewOfIndex:index];
}

#pragma mark -
#pragma mark 更新imageView
- (void)configImageViewOfIndex:(NSInteger)index {
    //更新背景图
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//    SplicingTemplateCollectionViewCell *cell = (SplicingTemplateCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//
//
//    UIView *animationView = cell.templateContentView;
//    CGRect rect = [animationView.superview convertRect:animationView.frame toView:nil];
//
//    XCPresentationScaleAnimation *animation = [[XCPresentationScaleAnimation alloc] init];
//    animation.animationView = animationView;
//    animation.sourceFrame   = rect;
//    animation.destFrame     = CGRectMake(16, 104, 382, 718);
//    animation.duration = 2;

    NSArray<UIImage *> *images = self.originImages;
    NSInteger styleIndex = indexPath.item+1;
    TSSplicingTemplateEditViewController *viewController = [[TSSplicingTemplateEditViewController alloc] initWithImages:images styleIndex:styleIndex];
    [self.navigationController pushViewController:viewController animated:YES];
    
//    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
//    [XCPresentation presentWithPresentationAnimation:animation presentedViewController:navigationController presentingViewController:self];
}

#pragma mark -
#pragma mark 手动切换方法
- (void)switchPrevious {
    NSInteger index = self.cardSwitch.selectedIndex - 1;
    index = index < 0 ? 0 : index;
    [self.cardSwitch switchToIndex:index animated:true];
}

- (void)switchNext {
    NSInteger index = self.cardSwitch.selectedIndex + 1;
    index = index > self.originImages.count - 1 ? self.originImages.count - 1 : index;
    [self.cardSwitch switchToIndex:index animated:true];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
